package com.ihemou.system.controller;

import com.ihemou.common.core.utils.poi.ExcelUtil;
import com.ihemou.common.core.web.controller.BaseController;
import com.ihemou.common.core.web.domain.AjaxResult;
import com.ihemou.common.core.web.page.TableDataInfo;
import com.ihemou.common.log.annotation.Log;
import com.ihemou.common.log.enums.BusinessType;
import com.ihemou.common.security.annotation.PreAuthorize;
import com.ihemou.common.security.annotation.ServiceRequest;
import com.ihemou.system.domain.SysLoginInfo;
import com.ihemou.system.service.ISysLoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 系统访问记录
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/logininfor")
public class SysLoginInfoController extends BaseController {
    @Autowired
    private ISysLoginInfoService logininforService;

    @PreAuthorize(hasPermi = "system:logininfor:list")
    @GetMapping("/list")
    public TableDataInfo list(SysLoginInfo logininfor) {
        startPage();
        List<SysLoginInfo> list = logininforService.selectLoginInfoList(logininfor);
        return getDataTable(list);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize(hasPermi = "system:logininfor:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLoginInfo logininfor) throws IOException {
        List<SysLoginInfo> list = logininforService.selectLoginInfoList(logininfor);
        ExcelUtil<SysLoginInfo> util = new ExcelUtil<SysLoginInfo>(SysLoginInfo.class);
        util.exportExcel(response, list, "登录日志");
    }

    @PreAuthorize(hasPermi = "system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable String[] infoIds) {
        return toAjax(logininforService.deleteLoginInfoByIds(infoIds));
    }

    @PreAuthorize(hasPermi = "system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        logininforService.cleanLoginInfo();
        return AjaxResult.success();
    }

    @ServiceRequest
    @PostMapping
    public AjaxResult add(@RequestBody SysLoginInfo loginInfo ) {
        return toAjax(logininforService.insertLoginInfo(loginInfo));
    }
}
